//
//  AudioBookViewController.m
//  AudioBook_A
//
//  Created by Sansan on 15/4/13.
//  Copyright (c) 2015年 Sansan. All rights reserved.
//

#import "AudioBookViewController.h"

@interface AudioBookViewController ()

@end

@implementation AudioBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"优声轻语";
    
    self.myViewScroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myViewScroll.backgroundColor = [UIColor colorWithRed:227/255.0 green:234/255.0 blue:246/255.0 alpha:1.0];
    self.myViewScroll.contentSize = CGSizeMake(ScreenFram.width, 860);
    [self.view addSubview:self.myViewScroll];
    [_myViewScroll release];
    
    self.view.backgroundColor = [UIColor colorWithRed:227/255.0 green:234/255.0 blue:246/255.0 alpha:1.0];
    
    //=============== 搜索 =======================
    
    
    
    
    //=============== 分类模块 ====================
    self.buttonView = [[ButtonsView alloc] initWithFrame:CGRectMake(0, 164, ScreenFram.width, 150)];
    self.buttonView.backgroundColor = [UIColor colorWithRed:227/255.0 green:234/255.0 blue:246/255.0 alpha:1.0];
    [self.myViewScroll addSubview:self.buttonView];
    [_buttonView release];
    
    // 添加一个轻拍手势
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.audioNovel addGestureRecognizer:tap2];
    self.buttonView.audioNovel.userInteractionEnabled = YES;
    self.buttonView.audioNovel.tag = 10002;
    
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.allType addGestureRecognizer:tap1];
    self.buttonView.allType.userInteractionEnabled = YES;
    self.buttonView.allType.tag = 10001;
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.musicType addGestureRecognizer:tap3];
    self.buttonView.musicType.userInteractionEnabled = YES;
    self.buttonView.musicType.tag = 10003;
    
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.entertainmentType addGestureRecognizer:tap4];
    self.buttonView.entertainmentType.userInteractionEnabled = YES;
    self.buttonView.entertainmentType.tag = 10004;
    
    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.comicType addGestureRecognizer:tap5];
    self.buttonView.comicType.userInteractionEnabled = YES;
    self.buttonView.comicType.tag = 10005;
    
    UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.emotionType addGestureRecognizer:tap6];
    self.buttonView.emotionType.userInteractionEnabled = YES;
    self.buttonView.emotionType.tag = 10006;
    
    UITapGestureRecognizer *tap7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.cultureType addGestureRecognizer:tap7];
    self.buttonView.cultureType.userInteractionEnabled = YES;
    self.buttonView.cultureType.tag = 10007;
    
    UITapGestureRecognizer *tap8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.baijiaType addGestureRecognizer:tap8];
    self.buttonView.baijiaType.userInteractionEnabled = YES;
    self.buttonView.baijiaType.tag = 10008;
    
    UITapGestureRecognizer *tap9 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.schoolType addGestureRecognizer:tap9];
    self.buttonView.schoolType.userInteractionEnabled = YES;
    self.buttonView.schoolType.tag = 10009;
    
    UITapGestureRecognizer *tap10 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(audioNovelTapAction:)];
    [self.buttonView.radioType addGestureRecognizer:tap10];
    self.buttonView.radioType.userInteractionEnabled = YES;
    self.buttonView.radioType.tag = 10010;
    
    [tap1 release];
    [tap2 release];
    [tap3 release];
    [tap4 release];
    [tap5 release];
    [tap6 release];
    [tap7 release];
    [tap8 release];
    [tap9 release];
    [tap10 release];
    
    //============ 精选无限循环 =================
    
    self.recommendView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenFram.width, 160)];
    self.recommendView.backgroundColor = [UIColor colorWithRed:217/255.0 green:224/255.0 blue:236/255.0 alpha:1.0];
    self.recommendView.contentSize = CGSizeMake(ScreenFram.width * 8, 0);
    [self.myViewScroll addSubview:self.recommendView];
    [_recommendView release];
    self.recommendView.pagingEnabled = YES;
    self.recommendView.showsHorizontalScrollIndicator = NO;
    self.recommendView.delegate = self;
    self.recommendView.contentOffset = CGPointMake(ScreenFram.width, 0);
    
    UILabel *albumTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 325, 300, 30)];
    albumTitle.text = @"推荐专辑";
    albumTitle.font = [UIFont systemFontOfSize:15];
    [albumTitle setTextColor:[UIColor colorWithRed:229/255.0 green:87/255.0 blue:40/255.0 alpha:1.0]];
    [self.myViewScroll addSubview:albumTitle];
    [albumTitle release];
    
    //===================推荐专辑======================
    
    self.albumTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 355, ScreenFram.width, 175) style:UITableViewStylePlain];
    [self.myViewScroll addSubview:self.albumTable];
    self.albumTable.delegate = self;
    self.albumTable.dataSource = self;
    self.albumTable.bounces = NO;
    
    self.albumsArray = [NSMutableArray array];
    
    [MyAFNData getNetData:@"http://mobile.ximalaya.com/m/super_explore_index?device=iPhone&includeActivity=true&picVersion=5&scale=2" responseGETData:^(id data) {
        
        NSMutableArray *array = [[data objectForKey:@"focusImages"] objectForKey:@"list"];
        
        for (NSInteger i = 1;i <= array.count;i++) {
            NSMutableDictionary *dic = [array objectAtIndex:i-1];
            NSString *picUrl = [dic objectForKey:@"pic"];
            
            UIImageView *albumImageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * ScreenFram.width + 10, 10, ScreenFram.width - 20, 140)];
            [albumImageView sd_setImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:nil];
            [self.recommendView addSubview:albumImageView];
            
            [albumImageView release];
        }
        UIImageView *albumImageViewLast = [[UIImageView alloc] initWithFrame:CGRectMake(7 * ScreenFram.width + 10, 10, ScreenFram.width - 20, 140)];
        NSString *urlLast = [[array objectAtIndex:0] objectForKey:@"pic"];
        [albumImageViewLast sd_setImageWithURL:[NSURL URLWithString:urlLast] placeholderImage:nil];
        
        [self.recommendView addSubview:albumImageViewLast];
        [albumImageViewLast release];
        
        UIImageView *albumImageViewFirst = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, ScreenFram.width - 20, 140)];
        NSString *urlFirst = [[array objectAtIndex:5] objectForKey:@"pic"];
        [albumImageViewFirst sd_setImageWithURL:[NSURL URLWithString:urlFirst] placeholderImage:nil];
        
        [self.recommendView addSubview:albumImageViewFirst];
        [albumImageViewFirst release];
        
        NSMutableArray *recomArray = [[data objectForKey:@"recommendAlbums"] objectForKey:@"list"];
        for (NSMutableDictionary *dic in recomArray) {
            recommendModel *model = [[recommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.albumsArray addObject:model];
            [model release];
        }
        [self.albumTable reloadData];
    }];
    
    //UIPagecontrol
    //显示页面页码的控件
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(40, 150, 300, 10)];
    self.pageC.backgroundColor = [UIColor clearColor];
    [self.myViewScroll addSubview:self.pageC];
    [_pageC release];
    self.pageC.currentPageIndicatorTintColor = [UIColor purpleColor];
    self.pageC.PageIndicatorTintColor = [UIColor colorWithRed:229/255.0 green:87/255.0 blue:40/255.0 alpha:1.0];
    
    self.pageC.numberOfPages = 6;
    self.pageC.currentPage = 0;
    
    //核心方法
    [self.pageC addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    
    //=================== 六选项有声播放 ===========================
    
    [MyAFNData getNetData:@"http://mobile.ximalaya.com/m/index_hotsounds?device=iPhone" responseGETData:^(id data) {
        
        self.segmentView = [[AudioChoiceView alloc] initWithFrame:CGRectMake(0, 535, ScreenFram.width, 260)];
        [self.myViewScroll addSubview:self.segmentView];
        
        self.segmentView.soundsDic = [NSMutableDictionary dictionary];
        
        NSMutableArray *array = [data objectForKey:@"categories"];
        for (NSMutableDictionary *dic in array) {
            NSMutableArray *typeArray = [NSMutableArray array];
            for (NSMutableDictionary *soundDic in [dic objectForKey:@"sounds"]) {
                soundsModel *model = [[soundsModel alloc] init];
                [model setValuesForKeysWithDictionary:soundDic];
                [typeArray addObject:model];
                [model release];
            }
            [self.segmentView.soundsDic setObject:typeArray forKey:[dic objectForKey:@"name"]];
        }
        [self.segmentView setMyImage];
    }];
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    recommendModel *model = [self.albumsArray objectAtIndex:indexPath.row];
    DetailAlbumsViewController *audioVC = [[DetailAlbumsViewController alloc] init];
    audioVC.ID = model.id;
    audioVC.title = model.title;
    [self.navigationController pushViewController:audioVC animated:YES];
    [audioVC release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.albumsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"recommentAlbumsCell";
    RecomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[RecomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    recommendModel *model = [self.albumsArray objectAtIndex:indexPath.row];
    cell.contentView.backgroundColor = [UIColor colorWithRed:217/255.0 green:224/255.0 blue:236/255.0 alpha:1.0];
    
    cell.model = model;
    
    return cell;
}

- (void)timeAction
{
    self.recommendView.contentOffset = CGPointMake(self.recommendView.contentOffset.x + ScreenFram.width, 0);
    int index = self.recommendView.contentOffset.x / ScreenFram.width;
    if (index == 7) {
        index = 1;
        self.recommendView.contentOffset = CGPointMake(ScreenFram.width, 0);
    }
    if (index == 0) {
        index = 6;
        self.recommendView.contentOffset = CGPointMake(6*ScreenFram.width, 0);
    }
    self.pageC.currentPage = index - 1;

}

- (void)changeValue:(UIPageControl *)page
{
    //改变偏移量 参数2为动画效果
    [self.recommendView setContentOffset:CGPointMake(self.recommendView.frame.size.width * page.currentPage, 0) animated:YES];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}



- (void)audioNovelTapAction:(UITapGestureRecognizer *)audioTap
{
    NSInteger tapValue = audioTap.view.tag;
    
    if (tapValue == 10001) {
        AudioTypeViewController *hotVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:hotVC animated:YES];
        hotVC.title = @"热门";
        hotVC.urlKey = @"";
        [hotVC release];
        
    } else if (tapValue == 10002) {
        
        AudioTypeViewController *audioNovelVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:audioNovelVC animated:YES];
        audioNovelVC.title = @"有声小说";
        audioNovelVC.urlKey = @"book";
        [audioNovelVC release];
        
    } else if (tapValue == 10003) {
        AudioTypeViewController *musicVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:musicVC animated:YES];
        musicVC.title = @"音乐";
        musicVC.urlKey = @"music";
        [musicVC release];
    } else if (tapValue == 10004) {
        
        AudioTypeViewController *enterVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:enterVC animated:YES];
        enterVC.title = @"综艺娱乐";
        enterVC.urlKey = @"entertainment";
        [enterVC release];
        
    } else if (tapValue == 10005) {
        
        AudioTypeViewController *comicVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:comicVC animated:YES];
        comicVC.title = @"相声评书";
        comicVC.urlKey = @"comic";
        [comicVC release];
        
    } else if (tapValue == 10006) {
        AudioTypeViewController *emotionVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:emotionVC animated:YES];
        emotionVC.title = @"情感生活";
        emotionVC.urlKey = @"emotion";
        [emotionVC release];
        
    } else if (tapValue == 10007) {
        AudioTypeViewController *cultureVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:cultureVC animated:YES];
        cultureVC.title = @"历史人文";
        cultureVC.urlKey = @"culture";
        [cultureVC release];
    } else if (tapValue == 10008) {
        
        AudioTypeViewController *baijiaVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:baijiaVC animated:YES];
        baijiaVC.title = @"百家讲坛";
        baijiaVC.urlKey = @"baijia";
        [baijiaVC release];
        
    } else if (tapValue == 10009) {
        AudioTypeViewController *schoolVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:schoolVC animated:YES];
        schoolVC.title = @"看校园";
        schoolVC.urlKey = @"xiaoyuan";
        [schoolVC release];
    } else if (tapValue == 10010) {
        AudioTypeViewController *radioVC = [[AudioTypeViewController alloc] init];
        [self.navigationController pushViewController:radioVC animated:YES];
        radioVC.title = @"电台";
        radioVC.urlKey = @"radio";
        [radioVC release];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
